package com.wjjm.spoonselling.service.impl;

import com.wjjm.spoonselling.dao.CartDao;
import com.wjjm.spoonselling.dao.CommodityDao;
import com.wjjm.spoonselling.pojo.Cart;
import com.wjjm.spoonselling.pojo.Commodity;
import com.wjjm.spoonselling.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    CartDao cartDao;

    @Autowired
    CommodityDao commodityDao;

    @Override
    public Boolean insert(Cart cart) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(date);
        cart.setDate(now);
        Boolean insert = cartDao.insert(cart);
        Integer commodityId = cart.getCommodityId();
        Commodity commodity = commodityDao.selectById(commodityId);
        commodity.setInventory(commodity.getInventory() - 1);
        int volume = commodity.getVolume();
//        System.out.println(++volume);
//        System.out.println(volume);
        commodity.setVolume(volume);
        int insert1 = commodityDao.update(commodity);

        if (insert && insert1 > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public List<Cart> selectByCustomerId(int customerId) {
        List<Cart> carts = cartDao.selectByCustomer(customerId);
        return carts;
    }
}
